package com.zc.angelica.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.angelica.entity.Account;
import com.zc.angelica.entity.ResponseData;
import com.zc.angelica.entity.RoleInfo;
import com.zc.angelica.mapper.RoleAccountInfoMapper;
import com.zc.angelica.entity.RoleAccountInfo;
import com.zc.angelica.service.RoleAccountInfoService;
import com.zc.angelica.service.RoleInfoService;
import com.zc.angelica.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * @author makejava
 * @since 2022-04-05 20:21:49
 */
@Service
public class RoleAccountInfoServiceImpl extends ServiceImpl<RoleAccountInfoMapper, RoleAccountInfo> implements RoleAccountInfoService {

    @Autowired
    private RoleInfoService roleInfoService;

    @Override
    public ResponseData select(Integer pageNo, Integer pageSize) {
        Account accountInfo = SecurityUtil.getAccountInfo();
        if (accountInfo != null) {
            if (pageNo != null && pageSize != null) {
                IPage<RoleInfo> pege = new Page<>(pageNo, pageSize);
                return ResponseData.success(this.baseMapper.selectPageInfo(pege, accountInfo.getId()));
            } else {
                return ResponseData.success(this.baseMapper.selectMyRolesInfo(accountInfo.getId()));
            }
        }

        return ResponseData.error("请检查登录状态，重新登录后再试！");
    }

    @Override
    public ResponseData binding(Long roleId) {
        RoleInfo roleInfo;
        if (roleId == null) {
            return ResponseData.error("绑定失败，角色id不能为空");
        } else {
            roleInfo = roleInfoService.getBaseMapper().selectById(roleId);
            if (roleInfo == null) {
                return ResponseData.error("绑定失败，角色不存在");
            }
        }
        Account accountInfo = SecurityUtil.getAccountInfo();
        if (accountInfo != null) {
            QueryWrapper<RoleAccountInfo> roleAccountInfoQueryWrapper = new QueryWrapper<>();
            roleAccountInfoQueryWrapper.eq("account_id", accountInfo.pkVal()).eq("role_id", roleId);
            List<RoleAccountInfo> roleAccountInfos = this.baseMapper.selectList(roleAccountInfoQueryWrapper);
            if (roleAccountInfos.size() > 0) {
                return ResponseData.error("角色已经绑定，请不要重复绑定");
            }
            RoleAccountInfo roleAccountInfo = new RoleAccountInfo();
            roleAccountInfo.setRoleId(roleId);
            roleAccountInfo.setAccountId(accountInfo.getId());
            roleAccountInfo.setRoleCode(roleInfo.getRoleCode());
            return ResponseData.success(this.baseMapper.insert(roleAccountInfo));

        }
        return ResponseData.error("请检查登录状态，重新登录后再试！");
    }

    @Override
    public ResponseData unbound(Serializable id) {
        if (id == null) {
            return ResponseData.error("解绑失败，角色id不能为空");
        }
        this.baseMapper.deleteById(id);
        return ResponseData.success("操作成功");
    }
}

